/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FotosPanel.java
 *
 * Created on 23/11/2011, 17:36:43
 */
package br.com.seguradora.view.panel;

import br.com.seguradora.model.Foto;
import br.com.seguradora.util.ImageFilter;
import br.com.seguradora.util.ImagePreview;
import java.awt.Cursor;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class FotosEmAnexoPanel extends javax.swing.JPanel {
    
    private JFileChooser fc;
    private List<Foto> fotosList = new ArrayList<Foto>();
    private static final String newline = "\n";
    private static final int LIMITE_FOTOS = 17;
    
    public FotosEmAnexoPanel() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        abrirPastaButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        logField = new javax.swing.JTextArea();
        qtdLabel = new javax.swing.JLabel();

        jLabel1.setText("Fotos:");

        abrirPastaButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/seguradora/resources/open16x16.png"))); // NOI18N
        abrirPastaButton.setToolTipText("Anexar fotos...");
        abrirPastaButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        abrirPastaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                abrirPastaButtonActionPerformed(evt);
            }
        });

        logField.setColumns(20);
        logField.setEditable(false);
        logField.setLineWrap(true);
        logField.setRows(5);
        logField.setWrapStyleWord(true);
        jScrollPane1.setViewportView(logField);

        qtdLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        qtdLabel.setText("0 fotos anexadas");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
                    .addComponent(qtdLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(abrirPastaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(qtdLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(abrirPastaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void abrirPastaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrirPastaButtonActionPerformed
        if (fotosList.size() > LIMITE_FOTOS) {
            JOptionPane.showMessageDialog(null, "O limite de fotos foi atingido.", "Mensagem do sistema", 2);
            return;
        }
        //Set up the file chooser.
        if (fc == null) {
            fc = new JFileChooser();
            fc.setMultiSelectionEnabled(true);

            //Add a custom file filter and disable the default
            //(Accept All) file filter.
            fc.addChoosableFileFilter(new ImageFilter());
            fc.setAcceptAllFileFilterUsed(false);
            fc.setApproveButtonText("OK");

            //Add the preview pane.
            fc.setAccessory(new ImagePreview(fc));
        }
        //Show it.
        int returnVal = fc.showOpenDialog(this);
        //Process the results.
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = fc.getSelectedFiles();
            anexarFotos(files);
            logField.setCaretPosition(logField.getDocument().getLength());
        } else {
            System.out.println("Attachment cancelled by user.");
        }

        //Reset the file chooser for the next time it's shown.
        fc.setSelectedFile(null);
}//GEN-LAST:event_abrirPastaButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton abrirPastaButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea logField;
    private javax.swing.JLabel qtdLabel;
    // End of variables declaration//GEN-END:variables

    private void anexarFotos(File[] files) {
        for (File file : files) {
            try {
                if (fotosList.size() <= LIMITE_FOTOS) {
                    logField.append("Anexando arquivo: " + file.getName() + "." + newline);
                    fotosList.add(new Foto(ImageIO.read(file)));
                    System.out.println("Attaching file: " + file.getName()
                            + ".");
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
        if (fotosList.size() == 1) {
            qtdLabel.setText("1 arquivo anexado");
        } else {
            qtdLabel.setText(fotosList.size() + " arquivos anexados");
        }
    }
    
    public List<Foto> getFotosList() {
        return fotosList;
    }
}
